package Web.Servlet;

import Domain.User;
import Service.Impl.UserServiceImpl;
import Service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");


        if(action.equals("Login")){
            Map<String, String[]> map = request.getParameterMap();

            User user = new User();

            try {
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            UserService service = new UserServiceImpl();
            List<User> list = service.UserLogin(user);
            User user1 = list.get(0);

            if(list.size() == 0){
                request.getRequestDispatcher("ErrorPage.jsp").forward(request,response);
            }

            if(list.size() == 1){

                if(user1.getC_question()==null){
                    request.setAttribute("user",user1);
                    request.getRequestDispatcher("UserSetSecurity.jsp").forward(request,response);
                    return;
                }

                request.setAttribute("user",user1);
                request.getRequestDispatcher("Index.jsp").forward(request,response);
            }
        }

        if(action.equals("Register")){
            Map<String, String[]> map = request.getParameterMap();


            User user = new User();

            try {
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            user.setHeader("moren.jpg");
            UserService service = new UserServiceImpl();
            service.AddUser(user);

            request.getRequestDispatcher("RegisterSuccessPage.jsp").forward(request,response);
        }



        if(action.equals("changePassword")){
            String ckcode = request.getParameter("ckcode");//验证码

            HttpSession session = request.getSession();
            String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");

            if(!checkcode_server.equalsIgnoreCase(ckcode)){
                //验证码不正确
                request.getRequestDispatcher("ErrorPage2.jsp").forward(request,response);
                return;
            }


            String username = request.getParameter("username");
            UserService service = new UserServiceImpl();
            List<User> list = service.FindUserByUsername(username);
            User user = list.get(0);
            request.setAttribute("user",user);
            request.getRequestDispatcher("User/forgetPwd2.jsp").forward(request,response);
        }

        if(action.equals("changePwd2")){
            String id = request.getParameter("id");

            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            String answer = request.getParameter("answer");

            if(!user.getC_answer().equals(answer)){
                //密保问题错误
                request.getRequestDispatcher("ErrorPage3.jsp").forward(request,response);
                return;
            }

            request.setAttribute("user",user);
            request.getRequestDispatcher("User/forgetPwd3.jsp").forward(request,response);

        }

        if(action.equals("changePwd3")){
            String password = request.getParameter("password");
            String password1 = request.getParameter("password1");
            String id = request.getParameter("id");
            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            if(!password.equals(password1)){
                request.getRequestDispatcher("ErrorPage4.jsp").forward(request,response);
                return;
            }

            service.changePassword(Integer.parseInt(id),password);
            request.getRequestDispatcher("User/forgetPwd4.jsp").forward(request,response);
        }

        if(action.equals("SetSecurity")){
            String id = request.getParameter("id");
            String question = request.getParameter("question");
            String answer = request.getParameter("answer");

            UserService service = new UserServiceImpl();
            service.SetSecurity(Integer.parseInt(id),question,answer);

            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("Index.jsp").forward(request,response);
        }

        if(action.equals("LogOut")){
            request.getRequestDispatcher("Index.jsp").forward(request,response);
        }

        if(action.equals("UserInf")){
            String id = request.getParameter("id");

            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("User/PersonInf.jsp").forward(request,response);

        }

        if(action.equals("ChangeUserInf")){
            String id = request.getParameter("id");
            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("User/ChangePersonInf.jsp").forward(request,response);
        }

        if(action.equals("SubmitChangeInf")){
            String id = request.getParameter("id");
            String header = request.getParameter("header");
            String oldheader = request.getParameter("oldheader");
            String username = request.getParameter("username");
            String tel = request.getParameter("tel");
            String address = request.getParameter("address");

            if(header.equals("") || header == null){
                header = oldheader;
            }

            UserService service = new UserServiceImpl();
            service.UpdateUserInf(Integer.parseInt(id),username,header,tel,address);

            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("User/ChangeInfSuccessPage.jsp").forward(request,response);
        }

        if(action.equals("BackToIndex")){
            String id = request.getParameter("id");
            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("Index.jsp").forward(request,response);
        }

        if(action.equals("BackToUserInf")){
            String id = request.getParameter("id");

            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.getRequestDispatcher("User/PersonInf.jsp").forward(request,response);
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
